
using com.tc.serial;
using com.tc.zigbee.mt.income;
using com.tc.zigbee.mt.request;

using System.Collections.Generic;
using com.tc.util;
using  com.tc.serial;
using com.tc.gateway.serial;
using com.tc.gateway.tunnel;
using  System.IO;
using  System;
using System.Threading;
namespace com.tc.zigbee.mt
{
    public class Monitor : ErrorListener, MessageListener
    {
        List<MessageListener> listeners = new List<MessageListener>();
        Thread sendThread;
        Thread receivThread;
        SerialStream stream;
        ErrorListener errorListener;
        public void addListener(MessageListener l)
        {
            listeners.Add(l);
        }
        public void onMessage(IncomeMessage msg)
        {
            foreach (MessageListener l in listeners)
                l.onMessage(msg);
        }
        public Monitor(SerialStream stream, TunnelOutput ts)
        {
            this.stream = stream;
            if (stream == null)
                return;
            receivThread = new Thread(new IncomingMessageReader(stream, this, this,
            ts).run);

            sendThread = new Thread(delegate()
            {
                while (true)
                {
                    MtCommand mc = RequestQueue.poll();
                    try
                    {
                        if (mc != null)
                            sendRequest(mc);

                        Thread.Sleep(300);
                    }
                    catch (Exception e)
                    {

                        this.receivThread.Abort();
                        if (errorListener != null)
                            errorListener.onException(e);
                        return;
                    }
                }

            });
        }

        public void start()
        {
            if (stream == null)
                return;
            this.receivThread.Start();
            this.sendThread.Start();
        }

        void sendRequest(MtCommand req)
        {
            if (stream == null)
                return;
            byte[] rf = req.frame;
            stream.getOutputStream().Write(rf, 0, rf.Length);
            stream.getOutputStream().Flush();


        }

        public void setErrorListener(ErrorListener errorListener)
        {
            this.errorListener = errorListener;
        }
        static public void main(String[] s)
        {

            //		Monitor m = new Monitor();
            //		ZigbeeManager zm = new ZigbeeManager();
            //		m.addListener(zm);
            //	
            //		m.start();
            //		MtCommand r = RequestFactory.MtIEEEAddrRequest((short)0x00, (byte)0, (byte)1);
            //		RequestQueue.commandqueue.offer(r);
        }
        public void onException(Exception e)
        {
            this.sendThread.Abort();
            this.errorListener.onException(e);


        }

    }
}
